import 'package:flutter/material.dart';
import 'package:free_music_player/models/song_model.dart';
import 'package:free_music_player/services/music_service.dart';

class MusicProvider with ChangeNotifier {
  final MusicService _musicService;
  bool _isInitialized = false;

  MusicProvider(this._musicService);

  // 初始化
  Future<void> init() async {
    if (!_isInitialized) {
      await _musicService.init();
      _isInitialized = true;
      notifyListeners();
    }
  }

  // 获取当前播放状态
  bool get isPlaying => _musicService.isPlaying;

  // 获取当前歌曲
  SongModel? get currentSong => _musicService.currentSong;

  // 获取播放列表
  List<SongModel> get playlist => _musicService.playlist;

  // 获取当前索引
  int get currentIndex => _musicService.currentIndex;

  // 获取播放模式
  int get playMode => _musicService.playMode;

  // 获取历史记录
  List<int> get history => _musicService.history;

  // 搜索歌曲
  Future<List<SongModel>> searchSongs(String keyword) async {
    return await _musicService.searchSongs(keyword);
  }

  // 获取歌曲详情
  Future<SongModel> getSongDetail(int songId) async {
    return await _musicService.getSongDetail(songId);
  }

  // 获取歌词
  Future<String> getLyric(int songId) async {
    return await _musicService.getLyric(songId);
  }

  // 设置播放列表并播放
  Future<void> setPlaylistAndPlay(List<SongModel> playlist, int index) async {
    await _musicService.setPlaylistAndPlay(playlist, index);
    notifyListeners();
  }

  // 播放当前歌曲
  Future<void> playCurrentSong() async {
    await _musicService.playCurrentSong();
    notifyListeners();
  }

  // 暂停播放
  Future<void> pause() async {
    await _musicService.pause();
    notifyListeners();
  }

  // 继续播放
  Future<void> resume() async {
    await _musicService.resume();
    notifyListeners();
  }

  // 下一首
  Future<void> nextSong() async {
    await _musicService.nextSong();
    notifyListeners();
  }

  // 上一首
  Future<void> previousSong() async {
    await _musicService.previousSong();
    notifyListeners();
  }

  // 切换播放模式
  void togglePlayMode() {
    _musicService.togglePlayMode();
    notifyListeners();
  }

  // 清除历史记录
  Future<void> clearHistory() async {
    await _musicService.clearHistory();
    notifyListeners();
  }

  // 释放资源
  void dispose() {
    _musicService.dispose();
    super.dispose();
  }
}